package org.iwidget.desktop.ui;
import org.iwidget.desktop.model.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import javax.swing.*;
/**
 *
 * @author Muhammad Hakim A
 */


public class DebugFrame extends JDialog
    implements ActionListener
{

    public DebugFrame(Hashtable widgetList)
    {
        this.widgetList = widgetList;
        setTitle("Iwidget Debug Console");
        setSize(600, 400);
        setLocation(100, 100);
        JButton buttonClear = new JButton("Clear");
        buttonClear.setActionCommand("clear");
        buttonClear.addActionListener(this);
        JButton buttonMemory = new JButton("Memory");
        buttonMemory.setActionCommand("memory");
        buttonMemory.addActionListener(this);
        JButton buttonImages = new JButton("Dump Widgets");
        buttonImages.setActionCommand("widgets");
        buttonImages.addActionListener(this);
        JPanel p = new JPanel();
        p.add(buttonClear);
        p.add(buttonMemory);
        p.add(buttonImages);
        area = new JTextArea();
        scroller = new JScrollPane();
        scroller.setVerticalScrollBarPolicy(20);
        scroller.setHorizontalScrollBarPolicy(30);
        scroller.getViewport().add(area);
        scroller.setAutoscrolls(true);
        getContentPane().setLayout(new BorderLayout());
        getContentPane().add(p, "North");
        getContentPane().add(scroller, "Center");
    }

    public void actionPerformed(ActionEvent e)
    {
        if(e.getActionCommand().equals("widgets"))
            dumpImages();
        else
        if(e.getActionCommand().equals("memory"))
            dumpMemory();
        else
        if(e.getActionCommand().equals("clear"))
            clearFrame();
    }

    public void dumpMemory()
    {
        addText("Memory");
        addText("-----------------------------");
        addText("Total:" + Runtime.getRuntime().totalMemory());
        addText("Free:" + Runtime.getRuntime().freeMemory());
        addText("\n");
    }

    public void dumpImages()
    {
        for(Enumeration e = widgetList.keys(); e.hasMoreElements(); addText("\n"))
        {
            WidgetElement widget = (WidgetElement)widgetList.get(e.nextElement());
            addText("Widget:" + widget.getWindowJS().getTitle());
            addText("    Dimensions:" + widget.getWindowJS().getX() + "," + widget.getWindowJS().getY() + "," + widget.getWindowJS().getHeight() + "," + widget.getWindowJS().getWidth());
            for(int iCtr = 0; iCtr < widget.uiObjects.size(); iCtr++)
            {
                Object objectJS = widget.uiObjects.get(iCtr);
                if(objectJS instanceof ElementImage)
                {
                    ElementImage ie = (ElementImage)objectJS;
                    addText("  Image:" + ie.getName());
                    addText("    Dimensions:" + ie.getX() + "," + ie.getY() + "," + ie.getHeight() + "," + ie.getWidth());
                    addText("    Visible:" + ie.getVisible());
                    addText("    Image:" + ie.getImage());
                    continue;
                }
                if(objectJS instanceof ElementTextField)
                {
                    ElementTextField ie = (ElementTextField)objectJS;
                    addText("  Text:" + ie.getName());
                    addText("    Dimensions:" + ie.getX() + "," + ie.getY() + "," + ie.getHeight() + "," + ie.getWidth());
                    addText("    Visible:" + ie.getVisible());
                    addText("    Data:" + ie.getData());
                    continue;
                }
                if(objectJS instanceof ElementTextArea)
                {
                    ElementTextArea ie = (ElementTextArea)objectJS;
                    addText("  TextArea:" + ie.getName());
                    addText("    Dimensions:" + ie.getX() + "," + ie.getY() + "," + ie.getHeight() + "," + ie.getWidth());
                    addText("    Visible:" + ie.getVisible());
                    addText("    Data:" + ie.getData());
                }
            }

        }

    }

    public void clearFrame()
    {
        area.setText("");
    }

    public void addText(String text)
    {
        area.append(text);
        area.append("\n");
        Point p = scroller.getViewport().getViewPosition();
        p.y += 100;
        scroller.getViewport().setViewPosition(p);
    }

    private static final long serialVersionUID = 0x3631343735393435L;
    private Hashtable widgetList;
    JTextArea area;
    JScrollPane scroller;
}